package vistasOficinaVenta;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import RMIClient.RMIClientOFV;
import beans.ClienteBean;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameUpdateCliente extends javax.swing.JInternalFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = 1L;
	private JTextField txtCUIT;
	private JTextField txtNombre;
	private JTextField txtPorcentajeDescuento;
	private JTextField txtCondicionVenta;
	private JLabel lblCUIT;
	private JLabel lblCondicion;
	private JButton btnAceptar;
	private JButton jButton1;
	private JButton btnCancelar;
	private JLabel lblNombre;
	private JLabel lblPorcDto;
	private static JFrameUpdateCliente instancia;
	

	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
		
	public static JFrameUpdateCliente getInstancia(){
		if (instancia==null){
			instancia = new JFrameUpdateCliente();
		}
		return instancia;
	}
	
	public JFrameUpdateCliente() {
		super();
		initGUI();
	}
	
	
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(400, 296));
			this.setBounds(0, 0, 400, 296);
			setVisible(true);
			this.setTitle("Modificacion Cliente");
			getContentPane().setLayout(null);
			{
				txtPorcentajeDescuento = new JTextField();
				getContentPane().add(txtPorcentajeDescuento);
				txtPorcentajeDescuento.setBounds(189, 122, 166, 21);
			}
			{
				lblPorcDto = new JLabel();
				getContentPane().add(lblPorcDto);
				lblPorcDto.setText("Porcentaje Descuento");
				lblPorcDto.setBounds(26, 125, 151, 14);
			}			
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(149, 12, 222, 21);
			}
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(189, 83, 166, 21);
			}
			{
				lblCondicion = new JLabel();
				getContentPane().add(lblCondicion);
				lblCondicion.setText("Condicion de Venta:");
				lblCondicion.setBounds(28, 161, 151, 14);
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("Ingrese CUIT: ");
				lblCUIT.setBounds(55, 15, 91, 14);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre Empresa:");
				lblNombre.setBounds(26, 87, 151, 14);
			}
				
			cargarTipoClientes();
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Obtener datos de Cliente");
				btnAceptar.setBounds(83, 44, 248, 20);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						ClienteBean clienteBean = RMIClientOFV.getInstancia().buscarCliente(txtCUIT.getText());
						if(clienteBean.getCuit()!=null && !"".equalsIgnoreCase(clienteBean.getCuit())){							
							txtPorcentajeDescuento.setText(String.valueOf(clienteBean.getDescuento()));
							txtNombre.setText(clienteBean.getNombreEmpresa());
							txtCondicionVenta.setText(clienteBean.getCondicionVenta());
							
							txtCUIT.setEditable(false);
						}else{
							JOptionPane.showMessageDialog(JFrameUpdateCliente.getInstancia(),"Cliente Inexistente");
						}
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(209, 216, 107, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1, "Center");
				jButton1.setText("Aceptar");
				jButton1.setBounds(73, 216, 98, 26);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						txtCUIT.setEditable(true);
						ClienteBean clienteBean = new ClienteBean();
						clienteBean.setCuit(txtCUIT.getText());
						//cargarTipoClienteSearch(clienteBean);							
						//cargarCondicionSearch(clienteBean);
						Float pjeDto = new Float("0");
						try{pjeDto = Float.parseFloat(txtPorcentajeDescuento.getText());}catch(Exception e){JOptionPane.showMessageDialog(JFrameAltaCliente.getInstancia(),"Porcentaje debe ser un numero");return;}
						if(pjeDto>100 || pjeDto<0){
							JOptionPane.showMessageDialog(JFrameUpdateCliente.getInstancia(),"Porcentaje debe ser un numero entre 0 y 100");
							return;
						}
						clienteBean.setDescuento(pjeDto);
						RMIClientOFV.getInstancia().updateCliente(clienteBean);
						JOptionPane.showMessageDialog(JFrameUpdateCliente.getInstancia(),"El cliente fue modificado exitosamente");
							
					}
				});
			}
			{
				txtCondicionVenta = new JTextField();
				getContentPane().add(txtCondicionVenta);
				txtCondicionVenta.setBounds(189, 158, 166, 20);
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void cargarTipoClienteSearch(ClienteBean clienteBean){
		String tipo = ((String)jComboBoxTipo.getSelectedItem());					
		clienteBean.setTipo(tipo.substring(0));
	}

	private void cargarCondicionSearch(ClienteBean clienteBean){
		String cond = ((String)jComboBoxCondicion.getSelectedItem());
		clienteBean.setCondicion(cond.substring(0));
	}

}
